package com.zmhz.details;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eova.aop.AopContext;
import com.eova.model.MetaObject;
import com.eova.service.sm;
import com.eova.widget.WidgetUtil;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.zmhz.widget.WidgetManager;

public class DetailsController extends Controller {
	final Controller ctrl = this;
	/**
	 * 公司介绍
	 */
	public void cominfo() {
		AopContext ac = new AopContext(ctrl);
		buildFormData(false, ac);
		render("/zmhz/details/cominfo.html");
	}
	/**
	 * 企业相册
	 */
	public void photo() {
		AopContext ac = new AopContext(ctrl);
		buildFormData(false, ac);
		render("/zmhz/details/cominfo.html");
	}
	/**
	 * 查看新闻、公告详情页
	 */
	public void news() {
		AopContext ac = new AopContext(ctrl);
		buildFormData(false, ac);
		render("/zmhz/details/pt_ob_zmnews.html");
	}
	/**
	 * 站内信
	 */
	public void letter() {
		AopContext ac = new AopContext(ctrl);
		buildFormData(false, ac);
		render("/zmhz/details/pt_ob_zmnews.html");
	}
	/**
	 * 供应产品信息详情页
	 */
	public void supply_gy() {
		AopContext ac = new AopContext(ctrl);
		buildFormData(false, ac);
		render("/zmhz/details/supply_gy.html");
	}
	/**
	 * 车队信息详情
	 */
	public void cargroup() {
		AopContext ac = new AopContext(ctrl);
		buildFormData(false, ac);
		render("/zmhz/details/cargroup.html");
	}
	/**
	 * 客运信息详情
	 */
	public void bus() {
		AopContext ac = new AopContext(ctrl);
		buildFormData(false, ac);
		render("/zmhz/details/bus.html");
	}
	/**
	 * 嫁接队信息详情
	 */
	public void jiajiedui() {
		AopContext ac = new AopContext(ctrl);
		buildFormData(false, ac);
		render("/zmhz/details/jiajiedui.html");
	}
	/**
	 * 基质穴盘信息详情
	 */
	public void jizhixuepai() {
		AopContext ac = new AopContext(ctrl);
		buildFormData(false, ac);
		render("/zmhz/details/jizhixuepai.html");
	}
	/**
	 * 更多
	 */
	public void more() {
		AopContext ac = new AopContext(ctrl);
		buildFormData(false, ac);
		render("/zmhz/details/more.html");
	}
	
	/**
	 * 构建对象数据
	 */
	private String buildFormData(boolean isEdit, AopContext ac) {
		String objectCode = this.getPara(0);
		// 获取主键的值
		Object pkValue = getPara(1);

		MetaObject object = sm.meta.getMeta(objectCode);
		// 根据主键获取对象
		Record record = Db.use(object.getDs()).findById(object.getView(), object.getPk(), pkValue);
		Integer viewsnum = record.getInt("viewsnum");
		if(null != viewsnum) {
			//更新浏览次数
			String sql = "update "+object.getView()+" set viewsnum =viewsnum+1 where id=?";
			Db.use(object.getDs()).update(sql,pkValue);
			record.set("viewsnum", record.getInt("viewsnum")+1);
		}
		List<Record> records = new ArrayList<Record>();
		records.add(record);
		// 备份Value列，然后将值列转换成Key列
		WidgetUtil.copyValueColumn(records, object.getPk(), object.getFields());
		// 根据表达式将数据中的值翻译成汉字
		WidgetManager.convertValueByExp(this, object.getFields(), records);
				
		setAttr("record", record);
		setAttr("object", object);

		ac.object = object;
		ac.record = record;

		return objectCode;
	}
	private String buildFormData(AopContext ac) {
		String objectCode = this.getPara(0);
		// 获取主键的值
		Object pkValue = getPara(1);

		MetaObject object = sm.meta.getMeta(objectCode);
		
		// 根据主键获取对象
		Record record = Db.use(object.getDs()).findById(object.getView(), object.getPk(), pkValue);
		
		setAttr("record", record);
		setAttr("object", object);

		ac.object = object;
		ac.record = record;

		return objectCode;
	}
}
